<template>
	<view class="wrap">
		<form @submit="_submit">
			<view class="top">
				<view class="item">
					<view class="left">收货人</view>
					<input type="text" placeholder-class="line" placeholder="请填写收货人姓名" name="username"
						v-model="username" />
				</view>
				<view class="item">
					<view class="left">手机号码</view>
					<input type="text" placeholder-class="line" placeholder="请填写收货人手机号" name="userphone"
						v-model="userphone" />
				</view>

				<view class="item" @click="regionVisible=true">
					<view class="left">所在地址</view>
					<input disabled type="text" placeholder-class="line" placeholder="省市区县、乡镇等" name="location"
						v-model="location" />
				</view>

				<view class="item address">
					<view class="left">详细地址</view>
					<textarea type="text" placeholder-class="line" placeholder="街道、楼牌等" name="useraddress"
						v-model="useraddress" />
				</view>
				<!-- <view class="site-clipboard">
					<textarea placeholder-class="line" value="" placeholder="粘贴文本,可自动识别姓名和地址等" />
					<view class="clipboard">
						地址粘贴板
						<u-icon name="arrow-down" class="icon" :size="20"></u-icon>
					</view>
				</view> -->
			</view>
			<view class="bottom">
				<view class="tag">
					<view class="left">标签</view>
					<view class="right">
						<u-radio-group v-model="label" @change="radioGroupChange" active-color="orange">
							<u-radio v-for="(item, index) in list" :key="index" :name="item.name">
								{{item.name}}
							</u-radio>
						</u-radio-group>
					</view>
				</view>
				<view class="default">
					<view class="left">
						<view class="set">设置默认地址</view>
						<view class="tips">提醒：每次下单会默认推荐该地址</view>
					</view>
					<view class="right">
						<switch color="red" @change="setDefault" name="status" :checked="status" />
					</view>
				</view>
			</view>
			<regionPicker :visible.sync="regionVisible" mode="region" default-type="value" :hide-area="false"
				@confirm="onConfirm($event,'region')" ref="region"></regionPicker>
			<button form-type="submit" type="warn">保存并使用</button>
			<button form-type="reset">重置</button>
		</form>
	</view>
</template>

<script>
	// 引入提示模块 
	let {
		load,
		hideload,
		isLogin,
		tip
	} = require('@/utils/tip.js');
	let app = getApp();
	import regionPicker from "@/components/region/w-picker.vue";

	export default {
		components: {
			regionPicker
		},

		data() {
			return {
				regionVisible: false, //初始化地区选择器开关	
				username: '', //初始化收件人姓名
				userphone: '', //初始化收件人手机号
				useraddress: '', //初始化收件人详情地址
				location: '山东省滨州市无棣县', //初始化地区默认值
				label: '家', //初始化地址标签
				list: [{
						name: '家'
					},
					{
						name: '公司'
					},
					{
						name: '学校'
					}
				],
				status: false,
				siteUrl: 'addressadd', //初始化请求url地址
				id: '', //初始化修改数据id
			};
		},
		onLoad(option) {
			if (option.type == 1) {
				let edit = uni.getStorageSync('address')
				this.username = edit.username
				this.useraddress = edit.useraddress
				this.userphone = edit.userphone
				this.location = edit.location
				this.label = edit.label
				this.status = edit.status == 1 ? true : false
			}
		},
		onBackPress(option) {
			if (option.from == 'backbutton') {
				// 自定义返回页面
				uni.navigateTo({
					url: '/pages/order/order'
				})
			} else {
				// 走默认返回
				return false
			}
		},
		methods: {
			async _submit(e) {
				// console.log(e);
				let {
					location,
					status,
					useraddress,
					username,
					userphone
				} = e.detail.value
				let {
					uid,
					token
				} = uni.getStorageSync('userInfo')
				let label = this.label
				let url = ''
				let data = {}
				if (this.$mp.query.type == 1) { //编辑地址
					url = '/api/addressedit',
						data = {
							id: uni.getStorageSync('address').id,
							location,
							status: status == true ? 1 : 0,
							useraddress,
							username,
							userphone
						}
				} else { //新增地址
					url = '/api/addressadd',
						data = {
							uid,
							location,
							status: status == true ? 1 : 0,
							useraddress,
							username,
							userphone
						}
				}
				let options = {
					url,
					data,
					method: 'POST',
					header: {
						authorization: token
					},
				}
				// console.log(options)
				let result = await this.$http(options)
				console.log(result);
				if (result.data.code == 200) {
					uni.showToast({
						title: result.data.msg,
						icon: 'success'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/address/address'
						})
					}, 2000)
				} else if (result.data.code == 403) {
					this.localBuffer = false
					uni.showToast({
						title: '请重新登录',
						icon: 'error'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 2000)
				} else {
					uni.showToast({
						title: '网络故障',
						icon: 'error'
					})
					this.localBuffer = false
				}
			},
			// 设置会员地区
			onConfirm(res) {
				this.location = res.result;

			},

			// 设置默认地址
			setDefault() {},

			// 设置底子标签
			radioGroupChange() {}

		}
	};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import '@/static/css/addsite.scss';
</style>